const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceProduct = require("../../../service/service_product.js");

module.exports = function(types){
  return {
    /**
     * behaviors
     */
    behaviors: [BehaviorAll],
    /**
     * data
     */
    data: {
      /**
       * 过滤属性
       * 数据格式：
          [
            { id: 1, name: "品牌",
              items: [{ id: 1, name: "a品牌" }, { id: 2, name: "b品牌" }]
            },
            { id: 2, name: "产地",
              items: [{ id: 3, name: "a产地" }, { id: 4, name: "b产地" }]
            },
            { id: 3, name: "容量",
              items: [{ id: 5, name: "a容量" }, { id: 6, name: "b容量" }]
            }
          ]
       */
      properties: [],
      /**
       * 产品
       * 数据格式：
          [
            { id: 1, name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6", price: 100,
              imgUrl: 'xxx.jpg',
              properties: [
                { id: 1, name: "葡萄酒" },
                { id: 2, name: "干红" },
                { id: 3, name: "750ml" },
                { id: 4, name: "法国" },
                { id: 5, name: "6只装" }
              ]
            }
          ]
       */
      products: [],
      /**
       * 产品过滤数据
       */
      filters: [],
      types: types,
      /**
       * 开始页
       */
      startPage: 0,
      /**
       * 购物车相关
       */
      car_amount: 0,
      triggerRetrieveData: false
    },

    /**
     * methods
     */
    methods: {
      onLoad: function (options) {
        const _this = this;
        // 列出产品
        ServiceProduct.page(0, _this.data.types, [], {}, function (data) {
          _this.setData({ products: data.records });
          const categoryId = data.data;
          // 列出产品分类对应的属性
          ServiceProduct.listProperties(categoryId, function (data) {
            if(data.records){
              _this.setData({ properties: data.records });
            }
          });
        });
      },
      /**
       * onShow
       */
      onShow: function (e) {
        const _this = this;
        _this.setData({ triggerRetrieveData: true });
      },
      /**
       * 获取购物车数据
       */
      car_onRetrieve: function (e) {
        const _this = this;
        _this.setData({
          car_amount: e.detail.amount
        })
      },
      /**
       * 触底事件
       */
      onReachBottom: function () {
        const _this = this;
        const filters = _this.data.filters;
        let startPage = this.data.startPage + 1;
        ServiceProduct.page(startPage, _this.data.types, filters, {}, function (data) {
          _this.data.products.push(...data.records);
          _this.setData({
            products: _this.data.products,
            startPage: startPage
          });
        });
      },
      /**
       * 提交过滤
       */
      onFilterSubmit: function(e){
        const _this = this;
        const data = e.detail.data;
        // 构造过滤参数
        let i = 0;
        const filters = [];
        for (let items of data) {
          let temp = filters[i++] = [];
          for (let item of items.items){
            if (item._selected === true){
              temp.push(item.id);
            }
          }
        }
        _this.setData({
          filters: filters,
          startPage: 0
        });
        // 申请数据
        ServiceProduct.page(_this.data.startPage, _this.data.types, filters, {}, function (data) {
          _this.setData({ products: data.records });
        });
      }
    }

  };
}